package com.example.markingsystem.controller;

import com.example.markingsystem.bean.externalteacher;
import com.example.markingsystem.bean.graduateinfo;
import com.example.markingsystem.bean.localteacher;
import com.example.markingsystem.bean.user;
import com.example.markingsystem.mapper.BackManagerMapper;
import com.example.markingsystem.mapper.LoginMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class BackManagementController {

    @Resource
    BackManagerMapper backManagerMapper;

    @Resource
    LoginMapper loginMapper;
    @RequestMapping("managerindex")
    public String managerindex(Model model){
        List<user> comList = backManagerMapper.getallcomuser();
        model.addAttribute("comList",comList);
        return "managerindex";
    }


    @RequestMapping("/lookuserinformation/{idcode}")
    public String lookuserinformation(@PathVariable("idcode")String idcode, Model model){
        //System.out.println(idcode);
        //判断是否填过了个人信息
        Boolean completely = loginMapper.checkidcode(idcode);
        if(!completely){
            //返回未找到信息
            return "nomessagereturn";
        }

        //获取用户个人信息
        else {
            char thirdDigit = idcode.charAt(idcode.length() - 3);
            int digit = Character.getNumericValue(thirdDigit);


            if(digit==1){
                localteacher message = backManagerMapper.benxiaogetalladminlook(idcode);
                //System.out.println(message);
                model.addAttribute("allmessage",message);
                return "adminmessagelook/benxiaolaoshil";
            }
            if(digit==5){
                graduateinfo message = backManagerMapper.yanjiugetalladminlook(idcode);
                //System.out.println(message);
                model.addAttribute("allmessage",message);
                return "adminmessagelook/yanjiushengl";
            }
            if(digit==8){
                externalteacher message = backManagerMapper.waixiaogetalladminlook(idcode);
                //System.out.println(message);
                model.addAttribute("allmessage",message);
                return "adminmessagelook/waixiaolaoshil";
            }

            return "nomessagereturn";
        }
    }

    //删除用户
    @RequestMapping("/deletecomuser/{idcode}")
    public String deletecomuser(@PathVariable("idcode")String idcode, Model model){
        //System.out.println(idcode);

        //直接删除用户，建议前端增加确认按钮。。。
        backManagerMapper.deletecomuser(idcode);
        //删除后返回
        List<user> comList = backManagerMapper.getallcomuser();
        model.addAttribute("comList",comList);
        return "managerindex";
    }

    @RequestMapping("admintoaddcommuser")
    public String admintoaddcommuser(){
        return "admintoaddcommuser";
    }

    //添加新的用户
    @RequestMapping("admintoaddcommuserto")
    public String admintoaddcommuserto(String idcode,int numberuse,Model model){
        int identity = 0;
        //System.out.println(idcode+numberuse);
        System.out.println(idcode.length());
        //判断输入是否合规
        if(idcode.length()!=8){
            model.addAttribute("errorm","禁止填入不合规内容！");
            return "admintoaddcommuser";
        }

        char thirdDigit = idcode.charAt(idcode.length() - 3);
        int digit = Character.getNumericValue(thirdDigit);
        if(digit!=1 && digit!=5 && digit!=8 ){
            model.addAttribute("errorm","身份码不合规！身份码的第六为必须是1，5，8中的数字！");
            return "admintoaddcommuser";
        }

        //判断是否存在这个身份码，防止报错
        Boolean ifexit = backManagerMapper.findidcodeexit(idcode);
        if(ifexit){
            model.addAttribute("errorm","这个身份码已经存在！");
            return "admintoaddcommuser";
        }
        backManagerMapper.adminaddnewcomuser(idcode,identity,numberuse);

        //结束后将数据回显...
        List<user> comList = backManagerMapper.getallcomuser();
        model.addAttribute("comList",comList);
        return "managerindex";
    }

    @RequestMapping("/updatenumberuse")
    public String updatenumberuse(String idcode,int numberuse,Model model){
        //System.out.println(idcode+numberuse);
        backManagerMapper.toupdatenumberuse(idcode,numberuse);

        List<user> comList = backManagerMapper.getallcomuser();
        model.addAttribute("comList",comList);
        return "managerindex";
    }

}
